using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.ProviderServices.Data;
using MimeCloud.Foundations.ObjectManagement;

namespace MimeCloud.AssetManagement.DataProvider.Sql
{
    public class Reader : IAssetReaderFactory
    {
        private static AssetManagerDataContext context
        {
            get
            {
                return Singleton<AssetManagerDataContext>.UserContextScoped; 
            }
        }

        public IAsset GetAssetByID(int id)
        {
            var astlist = (from ast in context.Assets where ast.ID == id select ast).ToList();
            if (astlist.Count == 0)
                return null;
            else return astlist[0];
        }

        public IList<IAssetContentType> GetAllAssetContentType()
        {
           // return new List<IAssetContentType>();
            return context.AssetContentTypes.Cast<IAssetContentType>().ToList();
        }

        public IList<ILanguage> GetAllLanguage()
        {
            return context.Languages.Cast<ILanguage>().ToList();
        }

        public IList<IMetaKey> GetAllMetaKey()
        {
            return context.MetaKeys.Cast<IMetaKey>().ToList();
        }

        public IList<IMimeType> GetAllMimeType()
        {
            return context.MimeTypes.Cast<IMimeType>().ToList();
        }

        public IMimeType GetMimeTypeByID(int id)
        {
            var mimlist = context.MimeTypes.Where(mim => mim.ID == id).ToList();
            if (mimlist.Count == 0)
                return null;
            else return mimlist[0];
        }

        public IList<IRepository> GetAllRepository()
        {
            return context.Repositories.Cast<IRepository>().ToList();
        }

        public IRepository GetRepositoryByID(int id)
        {
            var repolist = (from mim in context.Repositories where mim.ID == id select mim).ToList();
            if (repolist.Count == 0)
                return null;
            else return repolist[0];
        }

        public IList<IStatus> GetAllStatus()
        {
            return context.Statuses.Cast<IStatus>().ToList();
        }

        public IStatus GetStatusByID(int id)
        {
            var statlist = (from st in context.Statuses where st.ID == id select st).ToList();
            if (statlist.Count == 0)
                return null;
            else return statlist[0];
        }
    }
}
